package com.jk.cxh.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jk.OrderBean;
import com.jk.SeatBean;
import com.jk.UserBean;
import com.jk.UserInfo;
import com.jk.cxh.utils.UploadImg;
import com.jk.service.CxhUserServer;
import com.netflix.client.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author 程旭航
 * @Create 2021/5/21 - 10:29
 * @Description
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private CxhUserServer cxhUserServer;

    /**
     * @Author:程旭航
     * @Description: 获取验证码
     * @Date: 2021/5/21 14:20
     * @param phone
     * @Return: java.lang.String
     **/
    @GetMapping("sendCode")
    public String  sendCode(String phone,String typ){
        String s = cxhUserServer.sendCode(phone, typ);
        return s;
    }

    /**
     * @Author:程旭航
     * @Description: 快捷登录 忘记密码
     * @Date: 2021/5/21 16:18
     * @param
     * @Return:
     **/
    @GetMapping("quickLogin")
    public String  quickLogin(String phone, String code, String typ, HttpSession session){
        HashMap<String, Object> map =  cxhUserServer.quickLogin(phone,code,typ);
        String string = (String) map.get("string");
        ObjectMapper mapper = new ObjectMapper();
        UserBean bean = mapper.convertValue(map.get("bean"), UserBean.class);
        //UserBean bean = (UserBean) map.get("bean");
        session.setAttribute("user",bean);
        return string;
    }

    /** 
     * @Author:程旭航
     * @Description: 
     * @Date: 2021/5/21 16:50 
     * @param
     * @Return:
     **/
    @GetMapping("login")
    public String login( String userName,String userPwd, HttpSession session){
        HashMap<String, Object> map = cxhUserServer.login(userName, userPwd);
        String string = (String) map.get("string");
        ObjectMapper mapper = new ObjectMapper();
        UserBean bean = mapper.convertValue(map.get("bean"), UserBean.class);
        //UserBean bean = (UserBean) map.get("bean");
        session.setAttribute("user",bean);
        return string;
    }

    /**
     * @Author:程旭航
     * @Description: 修改密码
     * @Date: 2021/5/21 20:30
     * @param
     * @Return:
     **/
    @PostMapping("upPwd")
    public String upPwd(String phone,String userPwd){
        return cxhUserServer.upPwd(phone,userPwd);
    }

    /**
     * @Author:程旭航
     * @Description: 注册
     * @Date: 2021/5/25 16:50
     * @param phone
     * @param userPwd
     * @param code
     * @param typ
     * @Return: java.lang.String
     **/
    @PostMapping("addUser")
    public String addUser(String  phone,String  userPwd,String code,String typ){
        UserBean userBean = new UserBean();
        userBean.setPhone(phone);
        userBean.setUserPwd(userPwd);

        return cxhUserServer.addUser(userBean,code,typ);
    }


    /**
     * @Author:程旭航
     * @Description:
     * @Date: 2021/5/26 11:51
     * @param phone
     * @Return: List
     **/
    @GetMapping("queryOrder")
    public List<OrderBean>  queryOrder(String phone, Integer page, Integer rows){
        List<OrderBean> orderBeans = cxhUserServer.queryOrder(phone, page, rows);
        return orderBeans;
    }

    /**
     * @Author:程旭航
     * @Description: 上传图片
     * @Date: 2021/5/28 16:36
     * @param img
     * @Return: java.lang.String
     **/
    @RequestMapping("img")
    public String img(MultipartFile img) throws IOException, ClientException {
        return UploadImg.getUrl(img);
    }

    /**
     * @Author:程旭航
     * @Description: 修改信息
     * @Date: 2021/5/28 17:29
     * @param user
     * @Return: java.lang.String
     **/
    @PostMapping("updateUserInfo")
    public String updateUserInfo(UserInfo user){
       return  cxhUserServer.updateUserInfo(user);
    }

    @GetMapping("queryUserInfo")
    public UserInfo queryUserInfo(Integer userId){
        return cxhUserServer.queryUserInfo(userId);

    }

    /**
     * @Author:程旭航
     * @Description: 删除未支付订单
     * @Date: 2021/5/30 19:45
     * @param orderId
     * @param phone
     * @Return: void
     **/
    @PostMapping("delOrder")
    public void delOrder(String orderId,String phone){
        cxhUserServer.delOrder(orderId,phone);
    }




    /**
     * @Author:程旭航
     * @Description:
     * @Date: 2021/6/1 8:51
     * @param
     * @Return:
     **/
    @GetMapping("loginSession")
    public UserBean  loginSession(HttpSession session){
        UserBean user = (UserBean) session.getAttribute("user");


        return  user;
    }
}